﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ClientAppViaWebService.BaseLogicServiceRef;
using System.Windows.Forms;
using System.Drawing;

namespace ClientAppViaWebService.Utility
{
    public static class TreeViewLogic
    {     

        #region public static void InitTreeView(BaseLogicServiceRef.BaseLogicService service, ref TreeView treeView, ref ContextMenuStrip contextMenuStrip)
        public static void InitTreeView(BaseLogicServiceRef.BaseLogicService service, 
            ref TreeView treeView, 
            ref ContextMenuStrip contextMenuStrip )
        {
            treeView.Nodes.Clear();
            TreeNode t = new TreeNode("University");
            t.Tag = new TObject();
            t.ImageIndex = 36;
            t.NodeFont = new Font(new FontFamily("Times New Roman"), 12.0f);                       
            treeView.Nodes.Add(t);

            LoadChildrenByParentExtID(service, ref t, ((TObject)t.Tag).CLASS_ID.ToString(), ref contextMenuStrip);

            //Add item enabled
            contextMenuStrip.Items[0].Visible = true;
            t.ContextMenuStrip = contextMenuStrip;
        }
        #endregion

        #region public static void InitTreeViewByRootExtId(BaseLogicServiceRef.BaseLogicService service, ref TreeView treeView, ref ContextMenuStrip contextMenuStrip)
        public static void InitTreeViewByRootExtId(BaseLogicServiceRef.BaseLogicService service,
            ref TreeView treeView,
            ref ContextMenuStrip contextMenuStrip,
            string rootId)
        {

            treeView.Nodes.Clear();

            TObject[] list = service.SelectTObjectListByParentId(rootId);


            foreach (TObject t in list)
            {
                TreeNode treeNode = new TreeNode(t.NAME);
                treeNode.Tag = t;
                treeNode.ImageIndex = t.CLASS_ID;
                treeNode.Nodes.Add(string.Empty);
                treeNode.NodeFont = new Font(new FontFamily("Times New Roman"), 12.0f);
                treeNode.ContextMenuStrip = contextMenuStrip;
                treeView.Nodes.Add(treeNode);
                LoadChildrenByParentExtID(service, ref treeNode, ((TObject)treeNode.Tag).CLASS_ID.ToString(), ref contextMenuStrip);
            }            
        }
        #endregion        

        #region public static void InitTreeViewFilteredByClassIds.
        public static void InitTreeViewFilteredByClassIds(BaseLogicServiceRef.BaseLogicService service,
            ref TreeView treeView,
            ref ContextMenuStrip contextMenuStrip,
            int[] classIds)
        {

            treeView.Nodes.Clear();

            TObject[] list = service.SelectTObjectList_By_Class_Id(classIds);

            if (list.Length > 0)
            {
                TreeNode rootNode = new TreeNode(list[0].GROUP_NAME);
                rootNode.Tag = new TObject();
                rootNode.ImageIndex = list[0].CLASS_ID;
                rootNode.NodeFont = new Font(new FontFamily("Times New Roman"), 12.0f);
                
                foreach (TObject t in list)
                {
                    TreeNode treeNode = new TreeNode(t.NAME);
                    treeNode.Tag = t;
                    treeNode.ImageIndex = t.CLASS_ID;                  
                    treeNode.NodeFont = new Font(new FontFamily("Times New Roman"), 12.0f);
                    treeNode.ContextMenuStrip = contextMenuStrip;
                    rootNode.Nodes.Add(treeNode);
                }

                treeView.Nodes.Add(rootNode);              

                rootNode.Expand();
                //Add item disabled
                contextMenuStrip.Items[0].Visible = false;
            }
        }
        #endregion

        #region public static void InitTreeForSelectedNode.
        public static void InitTreeForSelectedNode(BaseLogicServiceRef.BaseLogicService service,
            ref TreeView treeView,
            ref ContextMenuStrip contextMenuStrip,
            string extId, TreeNode treeNodeToExpand)
        {
            if (extId == "0")
            {
                treeView.Nodes.Clear();

                TreeNode t = new TreeNode("University");
                t.Tag = new TObject();
                t.ImageIndex = 36;
                t.NodeFont = new Font(new FontFamily("Times New Roman"), 12.0f);                
                foreach (TreeNode node in treeNodeToExpand.Nodes)
                t.Nodes.Add(node);

                treeView.Nodes.Add(t);
                treeView.ExpandAll();
            }
            else
            {
                TObject parentNodeObj = service.SelectTObject_By_Ext_Id(extId);

                TreeNode parentNodeObjTreeNode = new TreeNode(parentNodeObj.NAME);
                parentNodeObjTreeNode.Tag = parentNodeObj;
                parentNodeObjTreeNode.ImageIndex = parentNodeObj.CLASS_ID;
                parentNodeObjTreeNode.Nodes.Add(string.Empty);
                parentNodeObjTreeNode.NodeFont = new Font(new FontFamily("Times New Roman"), 12.0f);
                parentNodeObjTreeNode.ContextMenuStrip = contextMenuStrip;

                LoadChildrenByParentExtID(service, ref parentNodeObjTreeNode, ((TObject)parentNodeObjTreeNode.Tag).CLASS_ID.ToString(), ref contextMenuStrip);
                parentNodeObjTreeNode.Expand();

                treeNodeToExpand = parentNodeObjTreeNode;
                TreeViewLogic.InitTreeForSelectedNode(service, ref treeView, ref contextMenuStrip, parentNodeObj.PARENT_ID, treeNodeToExpand);
             }
        }
        
        #endregion

        #region public static void LoadAddSubMenuStrip(ref ContextMenuStrip contextMenuStrip, string hostClass_Id)
        public static void LoadAddSubMenuStrip(ref ContextMenuStrip contextMenuStrip, string hostClass_Id)
        {

        }
        #endregion

        #region public static void LoadMenu(ref ContextMenuStrip menu, string class_Id)
        public static void LoadMenu(ref ContextMenuStrip menu, string class_Id)
        {
            // LoadAddSubMenuStrip for class_Id
        }
        #endregion

        #region public static void LoadChildrenByParentTobj(BaseLogicServiceRef.BaseLogicService service, TreeNode curr, string parent)
        public static void LoadChildrenByParentTobj(BaseLogicServiceRef.BaseLogicService service,
            ref TreeNode treeNodeParent,
            TObject parent,
            ref ContextMenuStrip contextMenuStrip)
        {

            if (service == null)
            {
                return;
            }
            LoadChildrenByParentExtID(service, ref treeNodeParent, 
                parent.EXT_ID == null ? "0" : parent.EXT_ID, ref contextMenuStrip);
        }
        #endregion

        #region public static void LoadChildrenByParentExtID(BaseLogicServiceRef.BaseLogicService service,
        public static void LoadChildrenByParentExtID(BaseLogicServiceRef.BaseLogicService service,
            ref TreeNode treeNodeParent,
            string rootId,
            ref ContextMenuStrip contextMenuStrip)
        {
            BaseLogicServiceRef.TObject[] list = service.SelectTObjectListByParentId(rootId);
            List<String> groups = new List<String>();

            treeNodeParent.Nodes.Clear();

            foreach (TObject t in list)
            {
                TreeNode treeNode = new TreeNode(t.NAME);
                treeNode.Tag = t;
                treeNode.Nodes.Add(string.Empty);
                treeNode.ContextMenuStrip = contextMenuStrip;

                if (!t.GROUP_NAME.Equals(String.Empty))
                {
                    Int32 grpIdx = groups.IndexOf(t.GROUP_NAME);
                    if (grpIdx == -1)
                    {
                        TreeNode grpNode = new TreeNode(t.GROUP_NAME);
                        grpNode.Tag = "-1";
                        grpNode.ImageIndex = t.CLASS_ID;
                        grpNode.ForeColor = Color.Blue;
                        grpNode.NodeFont = new Font(new FontFamily("Times New Roman"), 11.0f);
                        treeNodeParent.Nodes.Add(grpNode);
                        groups.Add(t.GROUP_NAME);
                        grpIdx = groups.Count - 1;
                    }
                    treeNodeParent.Nodes[grpIdx].Nodes.Add(treeNode);
                }
                //else
                //{
                //    treeNodeParent.Nodes.Add(treeNode);
                //}

            }
            /*UniViewFictiveNodePair[] groups = service.SelectGroupList(treeNodeParent.Tag as TObject);

            treeNodeParent.Nodes.Clear();
            foreach (UniViewFictiveNodePair p in groups)
            {
                TreeNode treeNode = new TreeNode(p.humanNamePl);
                treeNode.Tag = "-1";
                treeNodeParent.Nodes.Add(treeNode);
            }

            foreach (TObject t in list)
            {
                bool wuzAdded = false;
                TreeNode treeNode = new TreeNode(t.NAME);
                treeNode.Tag = t;
                treeNode.Nodes.Add(string.Empty);
                treeNode.ContextMenuStrip = contextMenuStrip;
                //treeNodeParent.Nodes.Add(treeNode);
                for (Int32 i = 0; i < groups.Length; ++i) // put to proper group
                {
                    if (t.CLASS_ID == groups[i].classId)
                    {
                        treeNodeParent.Nodes[i].Nodes.Add(treeNode);
                        wuzAdded = true;
                        break;
                    }
                }  
                if (!wuzAdded) // add unknown node
                {
                    treeNodeParent.Nodes.Add(treeNode);
                }
            }
             * */

        }
        #endregion


        public static TObject LoadNode(string ext_id, string parent_Id)
        {
            throw new Exception("Not implemented yet");
        }


    }
}
